package view.component.render
{
import core.model.PlayerModel;

import framework.view.components.container.RepeaterRenderProxy;

import values.item.INumbericItemValue;

import view.component.asset.IconProxy;

/**
 * ItemRequirementItemRender
 * @author jliang
 *
 */
public class ResourceRequirementItemRender extends RepeaterRenderProxy
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	public function ResourceRequirementItemRender()
	{
		super();
	}	
	//==========================================================================
	//  Overridden methods: RepeaterRenderProxy
	//==========================================================================	
	override public function set selected(value:Boolean):void
	{
		
	}
	
	override public function get selected():Boolean
	{
		return false;
	}
	
	protected var resource:INumbericItemValue;
	
	override public function set data(value:*):void
	{
		resource = value;
		
        init();
	}
	
	override public function get data():*
	{
		return resource;
	}
	
	override protected function get proxyMapping():Array
	{
		return [ 
			{ skin:"", classRef: IconProxy, id:"icon" }
		];
	}
	//==========================================================================
	//  Properties
	//==========================================================================
	[Inject]
	public var playerModel:PlayerModel;
	
	public function get currentNum():Number
	{
		return playerModel.getItemNum(resource.key);
	}
	
	public function get requirementNum():Number
	{
		return resource.num;
	}
	
	public function get isReached():Boolean
	{
		return currentNum >= requirementNum;
	}
	
	public function get icon():IconProxy
	{
		return proxys["icon"];
	}
    
    protected function get iconUrl():String
    {
        return resource.template.getIconPathByKey("icon");
    }
    //==========================================================================
    //  Public methods
    //==========================================================================
    public function update():void
    {
        skin.gotoAndStop(isReached ? 2 : 1);
        
        skin.label.text = currentNum + " / " + requirementNum;
    }
	//==========================================================================
	//  Protected methods
	//==========================================================================
	protected function init():void
	{
        icon.load(iconUrl);
        
        update();
	}
}
}




